/**
 * Title: TaskDistributionService.java
 *
 * @author wujin
 * @date 2019年12月30日
 * @version 1.0
 */
package com.pm.service.task.impl;

import com.pm.entity.po.task.*;
import com.pm.entity.vo.RepeatSubtasksVo;
import com.pm.entity.vo.StateVo;
import com.pm.entity.vo.task.TaskTargetVo;
import com.pm.mapper.task.TaskSubtasksMapper;
import com.pm.service.impl.BaseServiceImpl;
import com.pm.service.task.TaskSubtasksService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * Title: TaskDistributionService
 *
 * @author wujin
 * @date 2019年12月30日
 */
@Service
public class TaskSubtasksServiceImpl extends BaseServiceImpl<TaskSubtasks> implements TaskSubtasksService {
    private TaskSubtasksMapper taskSubtasksMapper;

    @Resource
    public void setBasemapper(TaskSubtasksMapper taskSubtasksMapper) {
        this.taskSubtasksMapper = taskSubtasksMapper;
        super.setBaseMapper(taskSubtasksMapper);
    }

    @Override
    public List<TaskSubtasks> getListTaskSubtasks(TaskSubtasks taskSubtasks) {
        if (taskSubtasks.getId() == null) {
            List<TaskSubtasks> taskSubtasks1 = taskSubtasksMapper.getListTaskSubtasks(taskSubtasks.getTaskId());
            if (taskSubtasks1.size() == 0) {
                return taskSubtasks1;
            }
            //判断有没有值
            List<TaskSubtasks> number = taskSubtasksMapper.sort(taskSubtasks.getTaskId());
            if (number.get(0).getAllNumberA() != 0) {
                //根据分值降序
                taskSubtasks1.sort(Comparator.comparing(TaskSubtasks::getAllNumberA).reversed());
                //根据分值升序
                //taskSubtasks1.sort(Comparator.comparing(TaskSubtasks::getAllNumberA));
                return taskSubtasks1;
            }
            if (number.get(0).getOneNumber() != 0) {
                //根据分值降序
                taskSubtasks1.sort(Comparator.comparing(TaskSubtasks::getAllNumberA).reversed());
                //根据分值升序
                //taskSubtasks1.sort(Comparator.comparing(TaskSubtasks::getAllNumberA));
                return taskSubtasks1;
            }
            if (number.get(0).getTwoNumber() != 0) {
                //根据分值降序
                taskSubtasks1.sort(Comparator.comparing(TaskSubtasks::getAllNumberA).reversed());
                //根据分值升序
                //taskSubtasks1.sort(Comparator.comparing(TaskSubtasks::getAllNumberA));
                return taskSubtasks1;
            }
            if (number.get(0).getThreeNumber() != 0) {
                //根据分值降序
                taskSubtasks1.sort(Comparator.comparing(TaskSubtasks::getAllNumberA).reversed());
                //根据分值升序
                //taskSubtasks1.sort(Comparator.comparing(TaskSubtasks::getAllNumberA));
                return taskSubtasks1;
            }

        }
        return taskSubtasksMapper.getListTaskSubtasksId(taskSubtasks.getId());
    }

    @Override
    public List<RepeatSubtasksVo> repeatSubtasks(String targetName) {
        return taskSubtasksMapper.repeatSubtasks(targetName);
    }

    @Override
    public List<RepeatSubtasksVo> repeat() {
        return taskSubtasksMapper.repeat();
    }

    @Override
    public List<StateVo> getstate(TaskSubtasks taskSubtasks) {
        if (taskSubtasks.getQuarter() == 1) {
            return taskSubtasksMapper.State1(taskSubtasks);
        }
        if (taskSubtasks.getQuarter() == 2) {
            return taskSubtasksMapper.State2(taskSubtasks);
        }
        if (taskSubtasks.getQuarter() == 3) {
            return taskSubtasksMapper.State3(taskSubtasks);
        }
        if (taskSubtasks.getQuarter() == 4) {
            return taskSubtasksMapper.State4(taskSubtasks);
        }
        return null;
    }

    @Override
    public Integer updateNew(TaskSubtasks taskSubtasks) {
        if (taskSubtasks.getQuarter() == 1) {
            return taskSubtasksMapper.updateNew1(taskSubtasks);
        }
        if (taskSubtasks.getQuarter() == 2) {
            return taskSubtasksMapper.updateNew2(taskSubtasks);
        }
        if (taskSubtasks.getQuarter() == 3) {
            return taskSubtasksMapper.updateNew3(taskSubtasks);
        }
        if (taskSubtasks.getQuarter() == 4) {
            return taskSubtasksMapper.updateNew4(taskSubtasks);
        }
        return null;
    }

    @Override
    public Integer updateAdopt(TaskSubtasks taskSubtasks) {
        if (taskSubtasks.getQuarter() == 1) {
            return taskSubtasksMapper.updateNew1(taskSubtasks);
        }
        if (taskSubtasks.getQuarter() == 2) {
            return taskSubtasksMapper.updateNew2(taskSubtasks);
        }
        if (taskSubtasks.getQuarter() == 3) {
            return taskSubtasksMapper.updateNew3(taskSubtasks);
        }
        if (taskSubtasks.getQuarter() == 4) {
            return taskSubtasksMapper.updateNew4(taskSubtasks);
        }
        return null;
    }

    //修改灯的状态
    @Override
    public Integer updateLamp(TaskSubtasks taskSubtasks) {
        if (taskSubtasks.getQuarter() == 1) {
            return taskSubtasksMapper.updateLamp1(taskSubtasks);
        }
        if (taskSubtasks.getQuarter() == 2) {
            return taskSubtasksMapper.updateLamp2(taskSubtasks);
        }
        if (taskSubtasks.getQuarter() == 3) {
            return taskSubtasksMapper.updateLamp3(taskSubtasks);
        }
        if (taskSubtasks.getQuarter() == 4) {
            return taskSubtasksMapper.updateLamp4(taskSubtasks);
        }
        return null;
    }

    @Override
    public Integer updateSocre(TaskSubtasks taskSubtasks) {
        if (taskSubtasks.getQuarter() == 1) {
            taskSubtasks.setLamp(0);
            return taskSubtasksMapper.updateSocre1(taskSubtasks);
        }
        if (taskSubtasks.getQuarter() == 2) {
            taskSubtasks.setLamp(0);
            return taskSubtasksMapper.updateSocre2(taskSubtasks);
        }
        if (taskSubtasks.getQuarter() == 3) {
            taskSubtasks.setLamp(0);
            return taskSubtasksMapper.updateSocre3(taskSubtasks);
        }
        if (taskSubtasks.getQuarter() == 4) {
            taskSubtasks.setLamp(0);
            return taskSubtasksMapper.updateSocre4(taskSubtasks);
        }
        return null;
    }

    @Override
    public List<TaskSubtasks> getListALL(TaskSubtasks taskSubtasks) {
        return taskSubtasksMapper.getListALL(taskSubtasks);
    }

    @Override
    public List<TaskSubtasks> getListQuarter(TaskSubtasks taskSubtasks) {
        if (taskSubtasks.getQuarter() == null) {
            return taskSubtasksMapper.getListALL(taskSubtasks);
        }
        if (taskSubtasks.getQuarter() == 1) {
            return taskSubtasksMapper.getListQuarter1(taskSubtasks);
        }
        if (taskSubtasks.getQuarter() == 2) {
            return taskSubtasksMapper.getListQuarter2(taskSubtasks);
        }
        if (taskSubtasks.getQuarter() == 3) {
            return taskSubtasksMapper.getListQuarter3(taskSubtasks);
        }
        if (taskSubtasks.getQuarter() == 4) {
            return taskSubtasksMapper.getListQuarter4(taskSubtasks);
        }
        return null;
    }

    @Override
    public List<Map<String, Object>> getLampProportion() {
        //红灯1
        Double redLamp = 0.0;
        Double redLampNum = 0.0;
        // 黄灯0
        Double yellowlamp = 0.0;
        Double yellowlampNum = 0.0;
        List<LampProportion> lampProportion = null;
        List<Map<String, Object>> lampProportionVos = new ArrayList<>();

        // 得到重点工作id
        List<Integer> taskIds = taskSubtasksMapper.getTaskIds();
        Map<String, Object> map = null;
        for (Integer taskId :
                taskIds) {
            //lampProportionVos.get(0).setTaskId(taskId);
            map = new HashMap<>();
            map.put("taskId", taskId);
            // AllNum 总数
            Integer AllNum = taskSubtasksMapper.getAllNum(taskId);
            String taskName = taskSubtasksMapper.gettaskName(taskId);
            map.put("taskName", taskName);
            lampProportion = taskSubtasksMapper.getLamp(taskId);
            // 统计数字
            for (LampProportion LampNum : lampProportion) {
                redLamp = 0.0;
                yellowlamp = 0.0;
                // 2红灯
                if (LampNum.getOneLamp() == 2) {
                    redLamp = redLamp + 1;
                }
                if (LampNum.getTwoLamp() == 2) {
                    redLamp = redLamp + 1;
                }
                if (LampNum.getThreeLamp() == 2) {
                    redLamp = redLamp + 1;
                }
                if (LampNum.getAllLamp() == 2) {
                    redLamp = redLamp + 1;
                }
                if (redLamp >= 2) {
                    redLampNum = redLampNum + 1;
                }
                // 1黄灯
                if (LampNum.getOneLamp() == 1) {
                    yellowlamp = yellowlamp + 1;
                }
                if (LampNum.getTwoLamp() == 1) {
                    yellowlamp = yellowlamp + 1;
                }
                if (LampNum.getThreeLamp() == 1) {
                    yellowlamp = yellowlamp + 1;
                }
                if (LampNum.getAllLamp() == 1) {
                    yellowlamp = yellowlamp + 1;
                }
                if (yellowlamp >= 2) {
                    yellowlampNum = yellowlampNum + 1;
                }
            }
            //lampProportionVos.get(0).setRed(AllNum / redLampNum);
            //lampProportionVos.get(0).setYellow(AllNum / yellowlampNum);
            map.put("red", redLampNum / AllNum);
            map.put("yellow", yellowlampNum / AllNum);
            lampProportionVos.add(map);
        }
        return lampProportionVos;
    }

    @Override
    public List<Map<String, Object>> getLampProportionNew(Integer taskId) {
        // 红灯数量
        Integer redLamp = 0;
        // 黄灯数量
        Integer yellowlamp = 0;
        LampProportion lampProportion = null;
        List<Map<String, Object>> lampProportionVos = new ArrayList<>();
        Map<String, Object> map = null;
        // 所有目标任务id
        List<Integer> subtasksIds = taskSubtasksMapper.getAllSubtasks(taskId);
        // 循环判断红黄灯数量
        for (Integer subtasksId :
                subtasksIds) {
            redLamp = 0;
            map = new HashMap<>();
            // 获得目标任务名称
            String targetName = taskSubtasksMapper.getTargetName(subtasksId);
            lampProportion = taskSubtasksMapper.getSubtasksLamp(subtasksId);
            // 2红灯
            if (lampProportion.getOneLamp() == 2) {
                redLamp = redLamp + 1;
            }
            if (lampProportion.getTwoLamp() == 2) {
                redLamp = redLamp + 1;
            }
            if (lampProportion.getThreeLamp() == 2) {
                redLamp = redLamp + 1;
            }
            if (lampProportion.getAllLamp() == 2) {
                redLamp = redLamp + 1;
            }
            // 如果红灯数大于2添加数据
            if (redLamp >= 2) {
                map.put("subtasksId", subtasksId);
                map.put("targetName", targetName);
                map.put("red", redLamp);
                lampProportionVos.add(map);
            }
        }
        return lampProportionVos;
    }

    @Override
    public List<TaskTargetPo> getTaskTargetFinishRate(TaskTargetVo taskTargetVo) {
        return taskSubtasksMapper.getTaskTargetFinishRate(taskTargetVo);
    }

    @Override
    public Integer getTaskSubtaskNum(Integer taskId) {
        return taskSubtasksMapper.getAllNum(taskId);
    }

    @Override
    public List<TaskTargetLightUpVo> getTaskTargetLightUpRate(TaskTargetVo taskTargetVo) {
        return taskSubtasksMapper.getTaskTargetLightUpRate(taskTargetVo);
    }

    @Override
    public Integer removeTaskByParentId(TaskSubtasks taskDistribution) {
        return taskSubtasksMapper.removeTaskByParentId(taskDistribution);
    }

    @Override
    public List<TaskSubtasks> getTaskSpeedList(TaskSubtasks taskDistribution) {
        return taskSubtasksMapper.getTaskSpeedList(taskDistribution);
    }

}

